//package com.lu.library
//
//import com.lu.library.http.ApiResponse
//import retrofit2.Call
//import retrofit2.CallAdapter
//import retrofit2.Response
//import retrofit2.Retrofit
//import java.lang.reflect.ParameterizedType
//import java.lang.reflect.Type
//
//class CatchingCallAdapterFactory(
//  val defaultErrorHandler: ErrorHandler? = null
//) : CallAdapter.Factory() {
//
//  // 用于配置全局的异常处理逻辑
//  interface ErrorHandler {
//    fun onBizError(errcode: Int, msg: String)
//    fun onOtherError(throwable: Throwable)
//  }
//
//  override fun get(
//    returnType: Type,
//    annotations: Array<out Annotation>,
//    retrofit: Retrofit
//  ): CallAdapter<*, *>? {
//    // suspend 函数在 Retrofit 中的返回值其实是 `Call`
//    // 例如：Call<ApiResponse<User>>
//    if (getRawType(returnType) != Call::class.java) return null
//    check(returnType is ParameterizedType)
//
//    // 取 Call 里边一层泛型参数
//    val innerType: Type = getParameterUpperBound(0, returnType)
//    // 如果不是 ApiResponse 则不由本 CallAdapter.Factory 处理
//    if (getRawType(innerType) != ApiResponse::class.java) return null
//
//    // 获取后续代理
//    val delegate: CallAdapter<*, *> = retrofit
//      .nextCallAdapter(this, returnType, annotations)
//
//    return CatchingCallAdapter(
//      innerType,
//      retrofit,
//      delegate,
//      defaultErrorHandler
//    )
//  }
//
//  class CatchingCallAdapter(
//    val dataType: Type,
//    val retrofit: Retrofit,
//    val delegate: CallAdapter<*, *>,
//    val errorHandler: ErrorHandler?
//  ) : CallAdapter<Any, Call<Any>> {
//    override fun responseType(): Type
//        = delegate.responseType()
//    override fun adapt(call: Call<Any>): Call<Any>
//        = CatchingCall(call, dataType as ParameterizedType, errorHandler)
//  }
//
//  class CatchingCall(
//    private val delegate: Call<Any>,
//    private val wrapperType: ParameterizedType,
//    private val errorHandler: ErrorHandler?
//  ) : Call<Any> {
//
//    override fun enqueue(
//      // suspend 其实是 callback
//      // suspend 的返回值通过这个 callback 传递
//      callback: Callback<Any>
//    ): Unit = delegate.enqueue(object : Callback<Any> {
//      override fun onResponse(call: Call<Any>, response: Response<Any>) {
//        // 无论请求响应成功还是失败都回调 Response.success
//        if (response.isSuccessful) {
//          val body = response.body()
//          if (body is ApiResponse.BizError<*>) {
//            errorHandler?.onBizError(body.errcode, body.msg)
//          }
//          callback.onResponse(this@CatchingCall, Response.success(body))
//        } else {
//          val throwable = HttpException(response.code(), response)
//          errorHandler?.onOtherError(throwable)
//          callback.onResponse(
//            this@CatchingCall,
//            Response.success(ApiResponse.OtherError(throwable))
//          )
//        }
//      }
//
//      override fun onFailure(call: Call<Any>, t: Throwable) {
//        errorHandler?.onOtherError(t)
//        callback.onResponse(
//          this@CatchingCall,
//          Response.success(ApiResponse.OtherError<Any>(t))
//        )
//      }
//    })
//
//    override fun clone(): Call<Any> =
//      CatchingCall(delegate, wrapperType, errorHandler)
//    override fun execute(): Response<Any> =
//      throw UnsupportedOperationException()
//    override fun isExecuted(): Boolean = delegate.isExecuted
//    override fun cancel(): Unit = delegate.cancel()
//    override fun isCanceled(): Boolean = delegate.isCanceled
//    override fun request(): Request = delegate.request()
//    override fun timeout(): Timeout = delegate.timeout()
//  }
//}